package cn.ezios.huo.system.biz.controller;

import cn.ezios.huo.system.dao.domain.SysUser;
import cn.ezios.huo.system.dao.dto.SysUserDTO;
import cn.ezios.huo.system.dao.vo.SysUserVO;
import cn.ezios.huo.system.dao.service.SysUserService;
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sys/user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    @PostMapping("")
    public Ret add(@RequestBody SysUserDTO sysUser) {
        boolean save = sysUserService.createUser(sysUser);
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody SysUserDTO sysUser) {
        boolean update = sysUserService.updateUser(sysUser);
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        SysUserVO sysUser = sysUserService.getUserById(id);
        return sysUser != null ? Ret.ok(sysUser) : Ret.error("未找到");
    }

    @GetMapping("page")
    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
    @RequestParam(value = "size", defaultValue = "10") Integer size,SysUserDTO dto) {
       IPage <SysUserVO> page = sysUserService.queryPage(current,size,dto);
            return Ret.ok(page);
    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
        boolean remove = sysUserService.removeById(id);
        return remove ? Ret.ok() : Ret.error();
    }
}
